<?php include_component('Category', 'categoriesList') ?>
<?php if(isset($items)): ?>
<?php
    $sortByFirst = $sf_request->getParameter('sortByFirst');
    $sortBySecond = $sf_request->getParameter('sortBySecond');
    $sortOrder = $sf_request->getParameter('sortOrder');
?>
<table id="inline_table">
    <tr>
    <form action="<?php echo link_to_app('Store',
                                         'categoryItemsListProxy',
                                         array(
                                               'categoryName' => $sf_request->getParameter('categoryName', $category->getName()),
                                               'sortByFirst' => $sf_request->getParameter('sortByFirst', 'name'),
                                               'sortBySecond' => $sf_request->getParameter('sortBySecond', 'name'),
                                               'sortOrder' => $sf_request->getParameter('sortOrder', 'ASC')
                                               )
                                         )
    ?>" method="post" id="sorting">
        <td colspan="<?php echo 3 + $columns->count() ?>">
            <?php for($i = 0; $i < 2; $i++): ?>
            <select name="sortBy<?php echo $i == 0 ? "First" : "Second" ?>" style="font-size: 12px; margin-left: 25px;">
                <option value="name" <?php if(($sortByFirst == "name" && $i == 0) || ($sortBySecond == "name" && $i == 1)) echo "selected=selected" ?>>Nazwa</option>
                <?php if(isset($columns)): ?>
                    <?php foreach($columns as $column): ?>
                    <option
                            value="<?php echo $column->getName() ?>"
                            <?php if(($sortByFirst == $column->getName() && $i == 0) || ($sortBySecond == $column->getName() && $i == 1)) echo "selected=selected" ?>
                    ><?php $s = explode("_", ucfirst($column->getName())); echo $s[0] ?></option>
                    <?php endforeach; ?>
                <?php endif; ?>
                <option value="quantity" <?php if(($sortByFirst == "quantity" && $i == 0) || ($sortBySecond == "quantity" && $i == 1)) echo "selected=selected" ?>>Ilość</option>
            </select>
            <?php endfor; ?>
        </td>
        <td colspan="2">
            <select name="sortOrder" style="font-size: 12px;">
                <option value="ASC" <?php if($sortOrder == "ASC") echo "selected=selected" ?>>Rosnąco</option>
                <option value="DESC" <?php if($sortOrder == "DESC") echo "selected=selected" ?>>Malejąco</option>
            </select>
        </td>
        <td>
            <input type="button" value="Sortuj" style="font-size: 12px; " onclick="App.Form.submit('sorting');"/>
        </td>
    </form>
    </tr>
    <tr>
    <th> Lp. </th>
    <th>
      Nazwa
      <!--<a href="<?php //echo link_to_app('Store', 'categoryItemsList', array('categoryName' => $sf_request->getParameter('categoryName', $category), 'sortBy' => 'name', 'sortOrder' => 'ASC')) ?>"><img src="/images/icons/sort_asc.png" class="middle" width="12" height="12"/></a> -->
    </th>
    <?php if(isset($columns)): ?>
    <?php foreach($columns as $column): ?>
    <th>
      <?php $s = explode("_", ucfirst($column->getName())); echo $s[0] ?> [ <?php echo $column->getUnit() ?> ]</a>
    </th>
    <?php endforeach; ?>
    <?php endif; ?>
    <th>
      Ilość [ <?php echo $quantityUnit ?> ]
    </th>
    <th>
      Kategoria
    </th>
    <th><a onclick="App.Table.toggleCheckAll('#inline_table')" class="active"> * </a></th>
    <th> Edycja </th>
    </tr>
    <?php if(count($items) > 0): ?>
    <tr id="fake"><td><form action="<?php echo link_to_app('Store', 'deleteItemList', array(), false) ?>" method="post" id="deleteList"></td></tr>
    <?php foreach($items as $key => $item): ?>
    <tr style='<?php if($sf_user->hasFlash("objectPage") && $item->getId() == $sf_request->getParameter("objectPage")) echo $sf_user->getFlash("objectPage") ?>'>
      <td><p><?php echo ($key + 1) + (($pager->getPage() - 1) * $pager->getMaxPerPage()) ?>.</p></td>
      <td style="text-align:left">
        <a onclick="App.loadWindow('<?php echo link_to_app('Store', 'showItem', array('id' => $item->getId()), false)?>', 600, 500)" class="active"><?php echo $item->getName() ?></a>
      </td>
      <?php if(isset($columns) && isset($itemsExtraFields)): ?>
      <?php foreach($columns as $column): ?>
      <td><?php echo $itemsExtraFields[$item->getId()][$column->getName()]  ?></td>
      <?php endforeach; ?>
      <?php endif; ?>
      <td>
        <?php echo $item->getQuantity() ? : "<span style=\"color:red\">0</span>"; ?>
      </td>
      <td>
        <a href="<?php echo link_to_app('Store', 'categoryItemsList', array('categoryName' => $item->getCategory())) ?>" class="middle"><?php echo $item->getCategory() ?></a>
      </td>
      <td><input type="checkbox" name="items_list[<?php echo $item->getId() ?>]"/></td>
      <td nowrap="nowrap">
        <?php if($sf_user->hasCredential('admin')): ?>
        <img src="/images/icons/change.png" class="middle" width="16" height="16"/>  <a onclick="App.Table.attachForm(this, '<?php echo link_to_app('Core', 'homeGetForm', array('form' => 'StoreItemForm', 'form_action' => 'update', 'category' => $category->getName(), 'id' => $item->getId())) ?>', '#inline_table', 'updateItem', <?php echo $parametersCount + 4 ?>, <?php echo $parametersCount + 2 ?>, true)" class="middle">Zmień</a>
        <img src="/images/icons/delete.png" class="middle" width="16" height="16"/>  <a href="<?php echo link_to_app('Store', 'deleteItem', array('id' => $item->getId())) ?>" class="middle">Usuń</a>
        <?php else: ?>
        <img src="/images/icons/change_inactive.png" class="middle" width="16" height="16"/> <a class="middle">Zmień</a>
        <img src="/images/icons/delete_inactive.png" class="middle" width="16" height="16"/> <a class="middle">Usuń</a>
        <?php endif; ?>
      </td>
    </tr>
    <?php endforeach; ?>
    <tr id="fake">
      <td></form></td>
    </tr>
    <tr id="add">
      <td colspan="<?php echo $parametersCount + 4 ?>">
        <?php if(!$category->getLocked()): ?>
        <img src="/images/icons/add.png" class="middle" width="16" height="16"/>  
        <a onclick="App.Table.attachForm(this, '<?php echo link_to_app('Core', 'homeGetForm', array('form' => 'StoreItemForm', 'form_action' => 'create', 'category' => $category->getName())) ?>', '#inline_table', 'createItem', <?php echo $parametersCount + 4 ?>, <?php echo $parametersCount + 2 ?>, true)" class="middle">Dodaj</a>
        <?php else: ?>
        Kategoria zablokowana. 
        <?php endif; ?>
      </td>
      <td colspan="2" nowrap="nowrap">
        <?php if($sf_user->hasCredential('admin')): ?>
        <img src="/images/icons/delete.png" class="middle" width="16" height="16"/>  
        <a onclick="App.Form.deleteChecked('deleteList', 'item')" class="middle">Usuń</a>
        <?php else: ?>
        <img src="/images/icons/delete_inactive.png" class="middle" width="16" height="16"/>  
        <a class="middle">Usuń</a>
        <?php endif; ?>
      </td>
    </tr>
    <?php else: ?>
    <tr>
      <td colspan="<?php echo $parametersCount + 6 ?>"> Brak pozycji w tej kategorii. </td>
    </tr>
    <tr id="fake">
      <td></form></td>
    </tr>
    <tr id="add">
      <td colspan="<?php echo $parametersCount + 6 ?>">
        <?php if(!$category->getLocked()): ?>
        <img src="/images/icons/add.png" class="middle" width="16" height="16" />  
        <a onclick="App.Table.attachForm(this, '<?php echo link_to_app('Core', 'homeGetForm', array('form' => 'StoreItemForm', 'form_action' => 'create', 'category' => $category->getName())) ?>', '#inline_table', 'createItem', <?php echo $parametersCount + 4 ?>, <?php echo $parametersCount + 2 ?>, true)" class="middle">Dodaj</a>
        <?php else: ?>
        Kategoria zablokowana.
        <?php endif; ?>
      </td>
    </tr>
    <?php endif; ?>
  </form>
    <?php if($pager->haveToPaginate()): ?>
    <th colspan="<?php echo count($items)+6 ?>">
        <a href="<?php echo link_to_app(
                                        'Store',
                                        'categoryItemsList',
                                        array(
                                              'categoryName' => $sf_request->getParameter('categoryName', $category),
                                              'sortByFirst' => $sf_request->getParameter('sortByFirst', 'name'),
                                              'sortBySecond' => $sf_request->getParameter('sortBySecond', 'name'),
                                              'sortOrder' => $sf_request->getParameter('sortOrder', 'ASC'))
                                        )?>?page=1"> Pierwsza </a>
        <a href="<?php echo link_to_app(
                                        'Store',
                                        'categoryItemsList',
                                        array(
                                              'categoryName' => $sf_request->getParameter('categoryName', $category),
                                              'sortByFirst' => $sf_request->getParameter('sortByFirst', 'name'),
                                              'sortBySecond' => $sf_request->getParameter('sortBySecond', 'name'),
                                              'sortOrder' => $sf_request->getParameter('sortOrder', 'ASC'))
                                        )?>?page=<?php echo $pager->getPreviousPage() ?>"> < </a>
        <?php foreach ($pager->getLinks() as $page): ?>
            <?php if ($page == $pager->getPage()): ?>
              <?php echo $page ?>
            <?php else: ?>
              <a href="<?php echo link_to_app(
                                              'Store',
                                              'categoryItemsList',
                                              array(
                                                    'categoryName' => $sf_request->getParameter('categoryName', $category),
                                              'sortByFirst' => $sf_request->getParameter('sortByFirst', 'name'),
                                              'sortBySecond' => $sf_request->getParameter('sortBySecond', 'name'),
                                                    'sortOrder' => $sf_request->getParameter('sortOrder', 'ASC'))
                                              )?>?page=<?php echo $page ?>"><?php echo $page ?></a>
            <?php endif; ?>
        <?php endforeach; ?>
        <a href="<?php echo link_to_app(
                                        'Store',
                                        'categoryItemsList',
                                        array(
                                              'categoryName' => $sf_request->getParameter('categoryName', $category),
                                              'sortByFirst' => $sf_request->getParameter('sortByFirst', 'name'),
                                              'sortBySecond' => $sf_request->getParameter('sortBySecond', 'name'),
                                              'sortOrder' => $sf_request->getParameter('sortOrder', 'ASC'))
                                        )?>?page=<?php echo $pager->getNextPage() ?>"> > </a>
        <a href="<?php echo link_to_app(
                                        'Store',
                                        'categoryItemsList',
                                        array(
                                              'categoryName' => $sf_request->getParameter('categoryName', $category),
                                              'sortByFirst' => $sf_request->getParameter('sortByFirst', 'name'),
                                              'sortBySecond' => $sf_request->getParameter('sortBySecond', 'name'),
                                              'sortOrder' => $sf_request->getParameter('sortOrder', 'ASC'))
                                        )?>?page=<?php echo $pager->getLastPage() ?>"> Ostatnia </a>
    </th>
    <?php endif; ?>
</table>
</form>
<?php endif; ?>
